﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace SmartDisplayControl
{
    public partial class SmartDisplayControl : UserControl
    {
        private HDrawingObject.HDrawingObjectCallback OnDetachCallBack;
        private HDrawingObject drawingObj;
        public HImage ho_Image;
        public HWindow hWindow = null;
        private int iLineWidthCur = -1;
      
        private string strColorCur = "";
        private string strFillModeCur = "";

        public delegate void DelegateDrawCircleArcFinished(double Row, double Col, double R, double startAngle, double endAngle);
        public delegate void DelegateDrawCircleFinished(double Row, double Col, double R);
        public delegate void DelegateDrawLineFinished(double dRow1, double dCol1, double dRow2, double dCol2);
        public delegate void DelegateDrawRectangle1Finished(double dRow1, double dCol1, double dRow2, double dCol2);
        public delegate void DelegateDrawRectangle2Finished(double Row, double Col, double Phi, double length1, double length2);
        public delegate void DelegateDrawXldFinished(double[] dRows, double[] dCols);

        public delegate void DelegateMouseUp(double Row, double Col);

        public event DelegateDrawCircleArcFinished drawCircleArcFinished;
        public event DelegateDrawCircleFinished drawCircleFinished;
        public event DelegateDrawLineFinished drawDrawLineFinished;
        public event DelegateDrawRectangle1Finished drawRectangle1Finished;
        public event DelegateDrawRectangle2Finished drawRectangle2Finished;
        public event DelegateDrawXldFinished drawXldFinished;

        public event DelegateMouseUp eventMouseUp;

       


        public SmartDisplayControl()
        {
            InitializeComponent();
        }

        private void hSmartWindowControl1_Load(object sender, EventArgs e)
        {
            try
            {
                this.hWindow = this.hSmartWindowControl1.HalconWindow;
                base.MouseWheel += new MouseEventHandler(this.SmartDisplayControl_MouseWheel);
                this.OnDetachCallBack = new HDrawingObject.HDrawingObjectCallback(this.DetachCallBackAction);
            }
            catch
            {
            }
        }
        private void SmartDisplayControl_MouseWheel(object sender, MouseEventArgs e)
        {
            Point location = base.Location;
            int x = this.hSmartWindowControl1.Location.X;
            int num2 = this.hSmartWindowControl1.Location.X + this.hSmartWindowControl1.Size.Width;
            int y = this.hSmartWindowControl1.Location.Y;
            int num4 = this.hSmartWindowControl1.Location.Y + this.hSmartWindowControl1.Size.Height;
            if ((((e.X > x) && (e.X < num2)) && (e.Y > y)) && (e.Y < num4))
            {
                MouseEventArgs args = new MouseEventArgs(e.Button, e.Clicks, e.X - location.X, e.Y - location.Y, e.Delta);
                this.hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, args);
            }
        }
        private void DetachCallBackAction(IntPtr intPtr1, IntPtr intPtr2, string strTemp)
        {
            if (drawingObj != null)
            {
                string drawingObjectParams = (string)this.drawingObj.GetDrawingObjectParams("type");
                if (drawingObjectParams.ToUpper() == "RECTANGLE1")
                {
                    double num = (double)this.drawingObj.GetDrawingObjectParams("row1");
                    double num2 = (double)this.drawingObj.GetDrawingObjectParams("column1");
                    double num3 = (double)this.drawingObj.GetDrawingObjectParams("row2");
                    double num4 = (double)this.drawingObj.GetDrawingObjectParams("column2");
                    if (drawRectangle1Finished != null)
                    {
                        this.drawRectangle1Finished(num, num2, num3, num4);
                    }
                }
                if (drawingObjectParams.ToUpper() == "RECTANGLE2")
                {
                    double row = (double)this.drawingObj.GetDrawingObjectParams("row");
                    double col = (double)this.drawingObj.GetDrawingObjectParams("column");
                    double phi = (double)this.drawingObj.GetDrawingObjectParams("phi");
                    double num8 = (double)this.drawingObj.GetDrawingObjectParams("length1");
                    double num9 = (double)this.drawingObj.GetDrawingObjectParams("length2");
                    if (drawRectangle2Finished != null)
                    {
                        this.drawRectangle2Finished(row, col, phi, num8, num9);
                    }
                }
                if (drawingObjectParams.ToUpper() == "CIRCLE")
                {
                    double num10 = (double)this.drawingObj.GetDrawingObjectParams("row");
                    double num11 = (double)this.drawingObj.GetDrawingObjectParams("column");
                    double r = (double)this.drawingObj.GetDrawingObjectParams("radius");
                    if (drawCircleFinished != null)
                    {
                        this.drawCircleFinished(num10, num11, r);
                    }
                }
                
                if (drawingObjectParams.ToUpper() == "CIRCLE_ARC")
                {
                    double num17 = (double)this.drawingObj.GetDrawingObjectParams("row");
                    double num18 = (double)this.drawingObj.GetDrawingObjectParams("column");
                    double num19 = (double)this.drawingObj.GetDrawingObjectParams("radius");
                    double startAngle = (double)this.drawingObj.GetDrawingObjectParams("start_angle");
                    double endAngle = (double)this.drawingObj.GetDrawingObjectParams("end_angle");
                    
                    if (drawCircleArcFinished != null)
                    {
                        this.drawCircleArcFinished(num17, num18, num19, startAngle, endAngle);
                    }
                }
                if (drawingObjectParams.ToUpper() == "LINE")
                {
                    double num = (double)this.drawingObj.GetDrawingObjectParams("row1");
                    double num2 = (double)this.drawingObj.GetDrawingObjectParams("column1");
                    double num3 = (double)this.drawingObj.GetDrawingObjectParams("row2");
                    double num4 = (double)this.drawingObj.GetDrawingObjectParams("column2");
                    if (drawDrawLineFinished != null)
                    {
                        this.drawDrawLineFinished(num, num2, num3, num4);
                    }
                }
                if (drawingObjectParams.ToUpper() == "XLD")
                {
                    HalconDotNet.HTuple hRows = drawingObj.GetDrawingObjectParams("row");
                    HalconDotNet.HTuple hCols = drawingObj.GetDrawingObjectParams("column");

                    if (drawXldFinished != null)
                    {
                        this.drawXldFinished(hRows.DArr, hCols.DArr);
                    }
                }
                this.drawingObj.Dispose();
                this.drawingObj = null;
            }
        }

        public void DisplayLine(double dRow1, double dCol1, double dRow2, double dCol2, string strColor = "red", int iLineWidth = 2)
        {
            this.SetDrawSetting(strColor, iLineWidth, "margin");
            this.hWindow.DispLine(dRow1, dCol1, dRow2, dCol2);
        }

        public void DisplayRectangle1(double dRow1, double dCol1, double dRow2, double dCol2, string strColor = "red", int iLineWidth = 2, string strFillMode = "margin")
        {
            this.SetDrawSetting(strColor, iLineWidth, strFillMode);
            this.hWindow.DispRectangle1(dRow1, dCol1, dRow2, dCol2);
        }
        public void SetDrawSetting(string strColor = "red", int iLineWidth = 2, string strFillMode = "margin")
        {
            //if (this.strColorCur != strColor)
            {
                this.hWindow.SetColor(strColor);
                this.strColorCur = strColor;
            }
            //if (this.iLineWidthCur != iLineWidth)
            {
                this.hWindow.SetLineWidth((double)iLineWidth);
                this.iLineWidthCur = iLineWidth;
            }
            //if (this.strFillModeCur != strFillMode)
            {
                this.hWindow.SetDraw(strFillMode);
                this.strFillModeCur = strFillMode;
            }
        }

        public void ShowImage(HImage hImage,bool bShowCenterLine=true)
        {
            if (hImage == null)
            {
                return;
            }
            if (bShowCenterLine)
            {
                if (this.ho_Image == null)
                {
                    int height = 0;
                    int width = 0;
                    hImage.GetImageSize(out width, out height);
                    this.ho_Image = hImage;
                    this.hWindow.SetPart(0, 0, height, width);
                    this.hWindow.ClearWindow();
                    this.hWindow.DispObj(this.ho_Image);
                    DisplayLine(0, width / 2, height, width / 2, "green");
                    DisplayLine(height / 2, 0, height / 2, width, "green");
                    hSmartWindowControl1.SetFullImagePart();
                }
                else
                {
                    int height = 0;
                    int width = 0;
                    hImage.GetImageSize(out width, out height);
                    this.ho_Image = hImage;
                    this.hWindow.ClearWindow();
                    this.hWindow.DispObj(this.ho_Image);
                    DisplayLine(0, width / 2, height, width / 2, "green");
                    DisplayLine(height / 2, 0, height / 2, width, "green");
                }
            }
            else
            {
                if (this.ho_Image == null)
                {
                    int height = 0;
                    int width = 0;
                    hImage.GetImageSize(out width, out height);
                    this.ho_Image = hImage;
                    this.hWindow.SetPart(0, 0, height, width);
                    this.hWindow.ClearWindow();
                    this.hWindow.DispObj(this.ho_Image);
                    
                    hSmartWindowControl1.SetFullImagePart();
                }
                else
                {
                    int height = 0;
                    int width = 0;
                    hImage.GetImageSize(out width, out height);
                    this.ho_Image = hImage;
                    this.hWindow.ClearWindow();
                    this.hWindow.DispObj(this.ho_Image);
                   
                }
            }
            
        }

        private void hSmartWindowControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Right) && (drawingObj != null))
            {
                //string drawingObjectParams = (string)this.drawingObj.GetDrawingObjectParams("type");
                //if (drawingObjectParams.ToUpper() == "RECTANGLE1")
                //{
                //    double num = (double)this.drawingObj.GetDrawingObjectParams("row1");
                //    double num2 = (double)this.drawingObj.GetDrawingObjectParams("column1");
                //    double num3 = (double)this.drawingObj.GetDrawingObjectParams("row2");
                //    double num4 = (double)this.drawingObj.GetDrawingObjectParams("column2");
                //    if (drawRectangle1Finished != null)
                //    {
                //        this.drawRectangle1Finished(num, num2, num3, num4);
                //    }
                //}
                //if (drawingObjectParams.ToUpper() == "RECTANGLE2")
                //{
                //    double row = (double)this.drawingObj.GetDrawingObjectParams("row");
                //    double col = (double)this.drawingObj.GetDrawingObjectParams("column");
                //    double phi = (double)this.drawingObj.GetDrawingObjectParams("phi");
                //    double num8 = (double)this.drawingObj.GetDrawingObjectParams("length1");
                //    double num9 = (double)this.drawingObj.GetDrawingObjectParams("length2");
                //    if (drawRectangle2Finished != null)
                //    {
                //        this.drawRectangle2Finished(row, col, phi, num8, num9);
                //    }
                //}
                //if (drawingObjectParams.ToUpper() == "CIRCLE")
                //{
                //    double num10 = (double)this.drawingObj.GetDrawingObjectParams("row");
                //    double num11 = (double)this.drawingObj.GetDrawingObjectParams("column");
                //    double r = (double)this.drawingObj.GetDrawingObjectParams("radius");
                //    if (drawCircleFinished != null)
                //    {
                //        this.drawCircleFinished(num10, num11, r);
                //    }
                //}
                
                //if (drawingObjectParams == "CIRCLE_ARC")
                //{
                //    double num17 = (double)this.drawingObj.GetDrawingObjectParams("row");
                //    double num18 = (double)this.drawingObj.GetDrawingObjectParams("column");
                //    double num19 = (double)this.drawingObj.GetDrawingObjectParams("radius");
                //    double startAngle = (double)this.drawingObj.GetDrawingObjectParams("start_angle");
                //    double endAngle = (double)this.drawingObj.GetDrawingObjectParams("end_angle");
                //    if (drawCircleArcFinished != null)
                //    {
                //        this.drawCircleArcFinished(num17, num18, num19, startAngle, endAngle);
                //    }
                //}
                this.hWindow.DetachDrawingObjectFromWindow(this.drawingObj);
                return;
            }
            if (eventMouseUp != null&& (e.Button == MouseButtons.Right))
            {
                eventMouseUp(e.Y, e.X);
            }
        }
        public void CreateRectangle1()
        {
            if ((this.ho_Image != null))
            {
                int num3;
                int num4;
                int num5;
                int num6;
                this.hWindow.ClearWindow();
                this.hWindow.GetPart(out num3, out num4, out num5, out num6);
                int num = num6 - num4;
                int num2 = num5 - num3;
                HTuple[] values = new HTuple[] { num3 + ((num2 * 2) / 5), num4 + ((num * 2) / 5), num5 - ((num2 * 2) / 5), num6 - ((num * 2) / 5) };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, values);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateRectangle1(int iRow1, int iCol1, int iRow2, int iCol2)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HTuple[] values = new HTuple[] { iRow1, iCol1, iRow2, iCol2 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, values);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }
        public void CreateRectangle2()
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                string strTypeCurImage;
                int iWidthCurImage, iHeightCurImage;
                ho_Image.GetImagePointer1(out strTypeCurImage, out iWidthCurImage, out iHeightCurImage);
                HalconDotNet.HTuple[] tupleParameter = { iHeightCurImage / 2, iWidthCurImage / 2, 0, iWidthCurImage / 5, iHeightCurImage / 5 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateRectangle2(int iRowC, int iColC, double dAngle, int iLeght1, int iLenght2)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HalconDotNet.HTuple[] tupleParameter = { iRowC, iColC, dAngle, iLeght1, iLenght2 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateCircle()
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                string strTypeCurImage;
                int iWidthCurImage, iHeightCurImage;
                ho_Image.GetImagePointer1(out strTypeCurImage, out iWidthCurImage, out iHeightCurImage);
                HalconDotNet.HTuple[] tupleParameter = { iHeightCurImage / 2, iWidthCurImage / 2,iHeightCurImage / 5 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateCircle(int iRowC, int iColC, double dR)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HalconDotNet.HTuple[] tupleParameter = { iRowC, iColC, dR };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateArc()
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                string strTypeCurImage;
                int iWidthCurImage, iHeightCurImage;
                ho_Image.GetImagePointer1(out strTypeCurImage, out iWidthCurImage, out iHeightCurImage);
                HalconDotNet.HTuple[] tupleParameter = { iHeightCurImage / 2, iWidthCurImage / 2, iHeightCurImage / 5, 0, Math.PI / 4 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateArc(double dRowC, int dColC, double dR,double dAngleStart, double dAngleEnd)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HalconDotNet.HTuple[] tupleParameter = { dRowC, dColC, dR, dAngleStart, dAngleEnd };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }
        public void CreateLine()
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                string strTypeCurImage;
                int iWidthCurImage, iHeightCurImage;
                ho_Image.GetImagePointer1(out strTypeCurImage, out iWidthCurImage, out iHeightCurImage);
                HalconDotNet.HTuple[] tupleParameter = { iHeightCurImage / 2-50, iWidthCurImage / 2 - 50, iHeightCurImage / 2 + 50, iWidthCurImage / 2 + 50 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateLine(double dRow1, double dCol1, double dRow2, double dCol2)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HalconDotNet.HTuple[] tupleParameter = { dRow1, dCol1, dRow2, dCol2 };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.LINE, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }
        public void CreateXLD()
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                string strTypeCurImage;
                int iWidthCurImage, iHeightCurImage;
                ho_Image.GetImagePointer1(out strTypeCurImage, out iWidthCurImage, out iHeightCurImage);
                HalconDotNet.HTuple tupleParameterRow = new HTuple(new double[] { iHeightCurImage / 2, iHeightCurImage / 2 + 50 });
                HalconDotNet.HTuple tupleParameterCol = new HTuple(new double[] { iWidthCurImage / 2, iWidthCurImage / 2 + 50 });
                HalconDotNet.HTuple[] tupleParameter = { tupleParameterRow, tupleParameterCol };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.XLD_CONTOUR, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }

        public void CreateXLD(double[] dRows, double[] dCols)
        {
            if ((this.ho_Image != null))
            {
                this.hWindow.ClearWindow();
                HalconDotNet.HTuple tupleParameterRow = new HTuple(dRows);
                HalconDotNet.HTuple tupleParameterCol = new HTuple(dCols);
                HalconDotNet.HTuple[] tupleParameter = { tupleParameterRow, tupleParameterCol };
                this.drawingObj = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.XLD_CONTOUR, tupleParameter);
                this.hWindow.AttachDrawingObjectToWindow(this.drawingObj);
                this.hWindow.AttachBackgroundToWindow(this.ho_Image);
                this.drawingObj.SetDrawingObjectParams("color", "green");
                HOperatorSet.SetDrawingObjectParams(this.drawingObj, "line_width", 3);
                this.drawingObj.OnDetach(this.OnDetachCallBack);
            }
        }
        public void DispObj(HalconDotNet.HObject obj, string strColor = "red", int iLineWidth = 2, string strFillMode = "margin")
        {
            this.SetDrawSetting(strColor, iLineWidth, "margin");
            hWindow.DispObj(obj);
        }
        public void DispString(string str, int row, int col, string strColor = "red", bool bAddToLists = true, string strFont = "Arial-Bold-20")
        {
            hWindow.SetColor(strColor);       
            try
            {
                hWindow.SetFont(strFont);          
            }
            catch
            {

            }
            hWindow.SetTposition(row, col);
            hWindow.WriteString(str);
        }
        public void DispString(string str, string strColor = "red", bool bAddToLists = true, string strFont = "Arial-Bold-20")
        {
            hWindow.SetColor(strColor);
            try
            {
                hWindow.SetFont(strFont);

            }
            catch
            {

            }
            hWindow.NewLine();
            hWindow.WriteString(str);
        }

        private void hSmartWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
        {
            if ((this.ho_Image == null))
            {
                return;
            }
            if ((e.Button == MouseButtons.Right) && (drawingObj != null))
            {
                this.hWindow.DetachDrawingObjectFromWindow(this.drawingObj);
                return;
            }
            if (eventMouseUp != null && (e.Button == MouseButtons.Right))
            {
                eventMouseUp(e.Y, e.X);
            }
        }

        public void DisplayFullLine(double dRow1, double dCol1, double dRow2, double dCol2, string strColor = "red")
        {
            if (ho_Image == null)
            {
                return;
            }

            hWindow.SetColor(strColor);


            int iWidth, iHeight;
            int iPointNum = 0;
            double row1 = dRow1;
            double row2 = dRow2;
            double col1 = dCol1;
            double col2 = dCol2;
            HTuple tRow1, tCol1, tOverlay1;
            HTuple tRow2, tCol2, tOverlay2;
            HTuple tRow3, tCol3, tOverlay3;
            HTuple tRow4, tCol4, tOverlay4;
            double dLineRow1 = 0, dLineCol1 = 0, dLineRow2 = 0, dLineCol2 = 0;
            ho_Image.GetImageSize(out iWidth, out iHeight);
            HOperatorSet.IntersectionSegmentLine(0, 0, 0, iWidth, row1, col1, row2, col2, out tRow1, out tCol1, out tOverlay1);
            HOperatorSet.IntersectionSegmentLine(0, 0, iHeight, 0, row1, col1, row2, col2, out tRow2, out tCol2, out tOverlay2);
            HOperatorSet.IntersectionSegmentLine(0, iWidth, iHeight, iWidth, row1, col1, row2, col2, out tRow3, out tCol3, out tOverlay3);
            HOperatorSet.IntersectionSegmentLine(iHeight, 0, iHeight, iWidth, row1, col1, row2, col2, out tRow4, out tCol4, out tOverlay4);
            if (tRow1.Length > 0)
            {
                if (tOverlay1 == 0)
                {
                    if (iPointNum == 0)
                    {
                        dLineRow1 = tRow1;
                        dLineCol1 = tCol1;
                    }
                    if (iPointNum == 1)
                    {
                        dLineRow2 = tRow1;
                        dLineCol2 = tCol1;
                    }
                    iPointNum = iPointNum + 1;
                }
            }
            if (tRow2.Length > 0)
            {
                if (tOverlay2 == 0)
                {
                    if (iPointNum == 0)
                    {
                        dLineRow1 = tRow2;
                        dLineCol1 = tCol2;
                    }
                    if (iPointNum == 1)
                    {
                        dLineRow2 = tRow2;
                        dLineCol2 = tCol2;
                    }
                    iPointNum = iPointNum + 1;
                }
            }
            if (tRow3.Length > 0)
            {
                if (tOverlay3 == 0)
                {
                    if (iPointNum == 0)
                    {
                        dLineRow1 = tRow3;
                        dLineCol1 = tCol3;
                    }
                    if (iPointNum == 1)
                    {
                        dLineRow2 = tRow3;
                        dLineCol2 = tCol3;
                    }
                    iPointNum = iPointNum + 1;
                }
            }
            if (tRow4.Length > 0)
            {
                if (tOverlay4 == 0)
                {
                    if (iPointNum == 0)
                    {
                        dLineRow1 = tRow4;
                        dLineCol1 = tCol4;
                    }
                    if (iPointNum == 1)
                    {
                        dLineRow2 = tRow4;
                        dLineCol2 = tCol4;
                    }
                    iPointNum = iPointNum + 1;
                }
            }
            hWindow.DispLine(dLineRow1, dLineCol1, dLineRow2, dLineCol2);
           
        }
        public void DispArrow(double dRow1, double dCol1, double dRow2, double dCol2, string strColor = "red",double dSize=1)
        {

            hWindow.SetColor(strColor);
            hWindow.DispArrow(dRow1, dCol1, dRow2, dCol2, dSize);
        }
        public void DispRectangle2(double rowCenter, double colCenter, double phi, double lenght1, double lenght2, string strColor = "green",bool bShowDir=false)
        {
            SetNormalSetting(strColor);
            HHomMat2D homMat2DInit = new HHomMat2D();
            HHomMat2D homMat2DRotate = homMat2DInit.HomMat2dRotate(-phi, 0, 0);
            HHomMat2D homMat2DTranslate = homMat2DRotate.HomMat2dTranslate(colCenter, rowCenter);
            HTuple dRow1Line, dCol1Line, dRow2Line, dCol2Line;
            HTuple dRow3Line, dCol3Line, dRow4Line, dCol4Line;
            if (bShowDir)
            {
                HOperatorSet.AffineTransPoint2d(homMat2DTranslate, lenght1, 0, out dCol1Line, out dRow1Line);
                HOperatorSet.AffineTransPoint2d(homMat2DTranslate, lenght1 + 20, 0, out dCol2Line, out dRow2Line);
                HOperatorSet.AffineTransPoint2d(homMat2DTranslate, 0, lenght2, out dCol3Line, out dRow3Line);
                HOperatorSet.AffineTransPoint2d(homMat2DTranslate, 0, lenght2 + 20, out dCol4Line, out dRow4Line);
                hWindow.DispArrow(dRow1Line, dCol1Line, dRow2Line, dCol2Line, 1);
                hWindow.DispArrow(dRow3Line, dCol3Line, dRow4Line, dCol4Line, 1);
            }
            hWindow.DispRectangle2(rowCenter, colCenter, phi, lenght1, lenght2);
        }
        protected void SetNormalSetting(string strColor,int iLineWidth=2)
        {
            hWindow.SetColor(strColor);
            hWindow.SetLineWidth(iLineWidth);
        }
        public void DispCircle(double rowCenter, double colCenter, double radius, string strColor = "red", bool bAddToLists = true)
        {
            hWindow.SetColor(strColor);
            hWindow.DispCircle(rowCenter, colCenter, radius);   
        }
        
    }
}
